3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace Wikimedia\Rdbms
;
23 use InvalidArgumentException
;
26 * Class to handle database/prefix specification for IDatabase domains
28 class DatabaseDomain
{
29 /** @var string|null */
31 /** @var string|null */
36 /** @var string Cache of convertToString() */
37 private $equivalentString;
40 * @param string|null $database Database name
41 * @param string|null $schema Schema name
42 * @param string $prefix Table prefix
44 public function __construct( $database, $schema, $prefix ) {
45 if ( $database !== null && ( !is_string( $database ) ||
!strlen( $database ) ) ) {
46 throw new InvalidArgumentException( "Database must be null or a non-empty string." );
48 $this->database
= $database;
49 if ( $schema !== null && ( !is_string( $schema ) ||
!strlen( $schema ) ) ) {
50 throw new InvalidArgumentException( "Schema must be null or a non-empty string." );
52 $this->schema
= $schema;
53 if ( !is_string( $prefix ) ) {
54 throw new InvalidArgumentException( "Prefix must be a string." );
56 $this->prefix
= $prefix;
60 * @param DatabaseDomain|string $domain Result of DatabaseDomain::toString()
61 * @return DatabaseDomain
63 public static function newFromId( $domain ) {
64 if ( $domain instanceof self
) {
68 $parts = array_map( [ __CLASS__
, 'decode' ], explode( '-', $domain ) );
73 if ( count( $parts ) == 1 ) {
74 $database = $parts[0];
75 } elseif ( count( $parts ) == 2 ) {
76 list( $database, $prefix ) = $parts;
77 } elseif ( count( $parts ) == 3 ) {
78 list( $database, $schema, $prefix ) = $parts;
80 throw new InvalidArgumentException( "Domain '$domain' has too few or too many parts." );
83 if ( $database === '' ) {
87 return new self( $database, $schema, $prefix );
91 * @return DatabaseDomain
93 public static function newUnspecified() {
94 return new self( null, null, '' );
98 * @param DatabaseDomain|string $other
101 public function equals( $other ) {
102 if ( $other instanceof DatabaseDomain
) {
104 $this->database
=== $other->database
&&
105 $this->schema
=== $other->schema
&&
106 $this->prefix
=== $other->prefix
110 return ( $this->getId() === $other );
114 * @return string|null Database name
116 public function getDatabase() {
117 return $this->database
;
121 * @return string|null Database schema
123 public function getSchema() {
124 return $this->schema
;
128 * @return string Table prefix
130 public function getTablePrefix() {
131 return $this->prefix
;
137 public function getId() {
138 if ( $this->equivalentString
=== null ) {
139 $this->equivalentString
= $this->convertToString();
142 return $this->equivalentString
;
148 private function convertToString() {
149 $parts = [ $this->database
];
150 if ( $this->schema
!== null ) {
151 $parts[] = $this->schema
;
153 if ( $this->prefix
!= '' ) {
154 $parts[] = $this->prefix
;
157 return implode( '-', array_map( [ __CLASS__
, 'encode' ], $parts ) );
160 private static function encode( $decoded ) {
163 $length = strlen( $decoded );
164 for ( $i = 0; $i < $length; ++
$i ) {
165 $char = $decoded[$i];
166 if ( $char === '-' ) {
168 } elseif ( $char === '?' ) {
178 private static function decode( $encoded ) {
181 $length = strlen( $encoded );
182 for ( $i = 0; $i < $length; ++
$i ) {
183 $char = $encoded[$i];
184 if ( $char === '?' ) {
185 $nextChar = $encoded[$i +
1] ??
null;
186 if ( $nextChar === 'h' ) {
189 } elseif ( $nextChar === '?' ) {
206 function __toString() {
207 return $this->getId();